//
//  DetailCell.m
//  吃货·上海
//
//  Created by lanou on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "DetailCell.h"
#import "UIImageView+WebCache.h"
#import "SHMenuModel.h"

@interface DetailCell () <UIScrollViewDelegate>


//背景图片
@property(nonatomic,strong)UIScrollView *backScr;
//动画的图片
@property(nonatomic,strong)UIImageView *iocn;

//标题
@property(nonatomic,strong)UILabel *title;
//简介
@property(nonatomic,strong)UILabel *info;
//制作方式
@property(nonatomic,strong)UILabel *myClass;
//食材
@property(nonatomic,strong)UILabel *mainMatcrials;
//制作方法
@property(nonatomic,strong)UILabel *makingMethod;

@property(nonatomic,strong)UILabel *makingmaterial;

@property (nonatomic,strong)UIView *view;

@end


@implementation DetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        
        //初始化进行布局设置
        
        //1 大的背景图
        CGFloat width = KScreenWidth;
        CGFloat imageHeight = 250;
        
        self.backScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, width, imageHeight)];
        
        self.backScr.delegate = self;
        self.backScr.minimumZoomScale = 1;
        self.backScr.maximumZoomScale = 1.5;
        self.backScr.userInteractionEnabled = NO;
        //动画的图片
        self.iocn =[[UIImageView alloc]initWithFrame:_backScr.frame];
        
        [self.backScr addSubview:_iocn];
        [self.contentView addSubview:_backScr];
        
        //定时器
        [self performSelector:@selector(zoomAnimation) withObject:nil afterDelay:0];
        
        self.timer  =[NSTimer scheduledTimerWithTimeInterval:6 target:self selector:@selector(zoomAnimation) userInfo:nil repeats:YES];
        
        //标题以及简介
        
        CGFloat title_Y = imageHeight + 5;
        CGFloat textWidth = KScreenWidth - 10;
        
        self.title =[[UILabel alloc]initWithFrame:CGRectMake(5, title_Y, textWidth, 40)];
//        self.title.textColor  =[UIColor whiteColor];
        self.title.font = [UIFont systemFontOfSize:16 weight:2];
        self.title.text = @"菜名 标题";
//        self.title.backgroundColor  =[UIColor blueColor];
        [self.contentView addSubview:_title];
        
        
        UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(5, title_Y + 40, 80, 20)];
        label1.text = @"#制作方式:";
        label1.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:label1];
        
        self.myClass = [[UILabel alloc]initWithFrame:CGRectMake(85, title_Y + 40, 60, 20)];
        self.myClass.text = @"蒸煮";
        [self.contentView addSubview:_myClass];
        
        UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(145, title_Y + 40, 80, 20)];
        label2.text = @"#制作食材:";
        label2.font = [UIFont systemFontOfSize:15];

        [self.contentView addSubview:label2];
        
        self.mainMatcrials = [[UILabel alloc]initWithFrame:CGRectMake(225, title_Y + 40, 60, 20)];
        self.mainMatcrials.text = @"面食";
        [self.contentView addSubview:_mainMatcrials];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(5, title_Y + 70, 50, 20)];
        label.text = @"简介:";
//        label.backgroundColor = [UIColor redColor];
        label.font = [UIFont systemFontOfSize:15];
        [self.contentView addSubview:label];
        
        //设置动态高度
    
        self.info = [[UILabel alloc]initWithFrame:CGRectMake(5, title_Y + 100, textWidth, 40)];
        //self.info.textColor = [UIColor whiteColor];
        self.info.numberOfLines = 0;
        self.info.font = [UIFont systemFontOfSize:14];
        
//        self.info.backgroundColor =[UIColor greenColor];
        [self.contentView addSubview:_info];

        self.view = [[UIView alloc]initWithFrame:CGRectMake(5, title_Y + 140, textWidth, 240)];
        [self.contentView addSubview:_view];
        
        //制作材料
        
        UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 80, 20)];
        label3.text = @"制作材料:";
        label3.font = [UIFont systemFontOfSize:14];
//        label3.backgroundColor =[UIColor redColor];
        [_view addSubview:label3];
        
        self.makingmaterial = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, textWidth, 100)];
        self.makingmaterial.numberOfLines = 0;
//        self.makingmaterial.textColor =[UIColor whiteColor];
        self.makingmaterial.font =[UIFont systemFontOfSize:14];
        [_view addSubview:_makingmaterial];
        
        //制作方法
        UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(0, 120, 80, 20)];
        label4.text = @"制作方法:";
        label4.font = [UIFont systemFontOfSize:14];
//        label4.backgroundColor =[UIColor redColor];
        [_view addSubview:label4];
        
        self.makingMethod = [[UILabel alloc]initWithFrame:CGRectMake(0, 140, textWidth, 100)];
//        self.makingMethod.textColor  =[UIColor whiteColor];
        self.makingMethod.numberOfLines = 0;
        self.makingMethod.font =[UIFont systemFontOfSize:14];
        [_view addSubview:_makingMethod];
    }
    
    return self;
}

//
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.iocn;
}


#pragma mark - timer
- (void)zoomAnimation
{
    [UIView animateWithDuration:3 animations:^{
        self.backScr.zoomScale = 1.12;
    }completion:^(BOOL finished) {
        if (finished) {
            [UIView animateWithDuration:3 animations:^{
                self.backScr.zoomScale = 1;
            }];
        }
    } ];
}

//赋值model
- (void)setModel:(SHMenuModel *)model
{
    _model = model;
    
    if (model.imageUrl != nil) {
         [self.iocn sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    }
    self.title.text = model.title;
    self.info.text = model.info;
    self.myClass.text = model.myClass;
    self.mainMatcrials.text = model.mainmatcrials;
    
    self.makingMethod.text = model.makingMethod;
    self.makingmaterial.text = model.makingmaterial;
    
    
    CGRect infoBounds = [_info.text boundingRectWithSize:CGSizeMake(KScreenWidth - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    
    CGRect makingMethodBounds = [_makingMethod.text boundingRectWithSize:CGSizeMake(KScreenWidth - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    
    CGRect makingmaterialBounds = [_makingmaterial.text boundingRectWithSize:CGSizeMake(KScreenWidth - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    
    //info -->frame
    CGFloat height = infoBounds.size.height;
    CGFloat Y = 355;
    
    self.info.frame  = CGRectMake(5, Y, KScreenWidth - 10 ,height);
    
    //view -->frame
    self.view.frame = CGRectMake(5, Y + height, KScreenWidth - 10, 240 + makingmaterialBounds.size.height + makingMethodBounds.size.height);
    
}

+(CGFloat)heightForCell:(SHMenuModel *)shMenuModel
{
    CGRect infoBounds = [shMenuModel.info boundingRectWithSize:CGSizeMake(KScreenWidth - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    
    CGRect makingMethodBounds = [shMenuModel.makingMethod boundingRectWithSize:CGSizeMake(KScreenWidth - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
    
    CGRect makingmaterialBounds = [shMenuModel.makingmaterial boundingRectWithSize:CGSizeMake(KScreenWidth - 10, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:14] forKey:NSFontAttributeName] context:nil];
       
    return 355 + infoBounds.size.height  + makingMethodBounds.size.height + makingmaterialBounds.size.height + 300;
}



@end
